package com.ossjk.oa.advisory.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ossjk.core.base.controller.BaseController;
import com.ossjk.core.util.CommonUtil;
import com.ossjk.oa.advisory.entity.School;
import com.ossjk.oa.advisory.service.ICounterpartService;
import com.ossjk.oa.advisory.service.ICounterpartfwService;
import com.ossjk.oa.advisory.service.ISchoolService;
import com.ossjk.oa.system.entity.Employee;
import com.ossjk.oa.system.service.IEmployeeService;

/**
 * <p>
 * 校园管理 前端控制器
 * </p>
 *
 * @author Jason
 * @since 2019-01-14
 */
@RestController
@RequestMapping("/advisory/school")
public class SchoolController extends BaseController {
	@Autowired
	private ISchoolService iSchoolService;
	@Autowired
	private ICounterpartService iCounterpartService;
	@Autowired
	private ICounterpartfwService iCounterpartfwService;
	@Autowired
	private IEmployeeService iEmployeeService;

	@RequiresPermissions("00150000")
	@GetMapping("/list")
	public Object list(String eid, String level, Page page) {
		Wrapper wrapper = Condition.create().isWhere(true).ne("s.isdelete", 2).orderBy("crtm");

		if (!CommonUtil.isBlank(eid)) {
			wrapper.like("s.eid", eid);
		}
		if (!CommonUtil.isBlank(level)) {
			wrapper.eq("s.level", level);
		}
		page = iSchoolService.selectVoPage(page, wrapper);
		return responseSuccess(page);
	}

	@RequiresPermissions("001500000020")
	@GetMapping("/getRecords")
	public Object getRecords(String cid, Page page) {
		Wrapper wrapper = Condition.create().isWhere(true).ne("cfw.isdelete", 2).orderBy("crtm");
		if (!CommonUtil.isBlank(cid)) {
			wrapper.eq("cfw.cid", cid);
		}
		page = iCounterpartfwService.selectVoPage(page, wrapper);
		return responseSuccess(page);
	}

	@RequiresPermissions("001500000000")
	@PostMapping("/insert")
	public Object insert(School school, String[] areaIds) {
		school.setPrid(areaIds[0]);
		school.setCtid(areaIds[1]);
		school.setArid(areaIds[2]);

		if (iSchoolService.insert(school)) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}

	@RequiresPermissions("001500000025")
	@GetMapping("/toUpdate/{id}")
	public Object toUpdate(@PathVariable("id") String id) {
		Map map = new HashMap();
		School school = iSchoolService.selectById(id);
		map.put("school", school);
		Employee employee = iEmployeeService.selectById(school.getEid());
		map.put("employee", employee);
		Wrapper wrapper = Condition.create().isWhere(true).orderBy("crtm", true);
		if (!CommonUtil.isBlank(id)) {
			wrapper.eq("sid", id);
		}
		List list = iCounterpartService.selectVo(wrapper);
		map.put("counterpart", list);
		// 回显省市区
		List areaIds = new ArrayList();
		areaIds.add(school.getPrid());
		areaIds.add(school.getCtid());
		areaIds.add(school.getArid());
		map.put("areaIds", areaIds);
		return responseSuccess(map);
	}

	@RequiresPermissions("001500000025")
	@PutMapping("/update")
	public Object update(School school, String[] areaIds) {
		school.setPrid(areaIds[0]);
		school.setCtid(areaIds[1]);
		school.setArid(areaIds[2]);
		if (iSchoolService.updateById(school)) {
			return responseSuccess();
		} else {
			return responseFail();
		}

	}

	@RequiresPermissions("001500000005")
	@DeleteMapping("/delete/{id}")
	public Object delete(@PathVariable("id") String[] id) {
		if (iSchoolService.deleteBatchIds(Arrays.asList(id))) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}

}
